﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MuddyBoots.GreenlightQC.DataService.Model
{
    public class Question : IDataErrorInfo
    {
        public string QuestionText { get; set; }

        public string DisplayQuestion
        {
            get
            {
                return this.IsRequired ? string.Format("{0} *", this.QuestionText) : this.QuestionText;
            }
        }

        public Type Type { get; set; }

        public bool IsRequired { get; set; }

        public object Answer { get; set; }

        public string Error
        {
            get
            {
                StringBuilder error = new StringBuilder();

                // iterate over all of the properties of this object - aggregating any validation errors
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
                foreach (PropertyDescriptor prop in props)
                {
                    string propertyError = this[prop.Name];
                    if (propertyError != string.Empty)
                    {
                        error.Append((error.Length != 0 ? ", " : "") + propertyError);
                    }
                }

                return error.ToString();
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (columnName == "Answer" && this.IsRequired)
                {
                    if (this.Answer == null || string.IsNullOrWhiteSpace(this.Answer.ToString()))
                    {
                        return "An answer to this question is required";
                    }
                }

                return string.Empty;
            }
        }
    }
}
